package com.ygq.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ygq.seckill.entity.Order;
import com.ygq.seckill.entity.SeckillGoods;
import com.ygq.seckill.entity.SeckillOrder;
import com.ygq.seckill.entity.User;
import com.ygq.seckill.mapper.OrderMapper;
import com.ygq.seckill.service.OrderService;
import com.ygq.seckill.service.SeckillGoodsService;
import com.ygq.seckill.service.SeckillOrderService;
import com.ygq.seckill.util.MD5Util;
import com.ygq.seckill.util.UUIDUtil;
import com.ygq.seckill.vo.GoodsVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private SeckillGoodsService seckillGoodsService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private SeckillOrderService seckillOrderService;
    @Resource
    private RedisTemplate redisTemplate;
    //秒杀商品，减少库存
    @Transactional
    //开启事务机制
    @Override
    public Order seckill(User user,
                         GoodsVo goodsVo) {

//查询后端的库存量进行减一
        SeckillGoods seckillGoods =
                seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>()
                        .eq("goods_id", goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
        System.out.println("update-->"+user.getId()+" "+goodsVo.getId());
        //在默认的事务隔离repeatable read,update语句会在事务中锁定更新的行
        //这可以防止其他会话在同一行上执行 UPDATE 或 DELETE 操作
        boolean update = seckillGoodsService.update
                (new UpdateWrapper<SeckillGoods>().setSql("stock_count = stock_count-1")
                                .eq("goods_id", goodsVo.getId()).gt("stock_count", 0));

        if(!update){
            return null;
        }


//生成普通订单
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);
//生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrderService.save(seckillOrder);
        //将生成的秒杀订单,存入 redis, 这样在查询某个用户是否已经秒杀过该商品时,直接在redis检查
        redisTemplate.opsForValue().set("order:"+user.getId()+":"+goodsVo.getId(),seckillOrder);

        return order;
    }

    //生成各自的path并放在redis,60s更新
    @Override
    public String createPath(User user, Long goodsId) {

        String str = MD5Util.md5(UUIDUtil.uuid());
        redisTemplate.opsForValue().set("seckillPath:"+user.getId()+":"+goodsId,str,60, TimeUnit.SECONDS);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {
       if(user==null||goodsId<0|| !StringUtils.hasText(path)){
           return false;
       }
        //path 判断
        String str = (String) redisTemplate.opsForValue().
                get("seckillPath:"+user.getId()+":"+goodsId);
        return path.equals(str);
    }
    @Override
    public boolean checkCaptcha(User user, Long goodsId, String captcha) {
        if (user == null || goodsId < 0 || !StringUtils.hasText(captcha)) {
            return false;
        }
        String redisCaptcha =
                (String) redisTemplate.opsForValue()
                        .get("captcha:" + user.getId() + ":" + goodsId);
        return captcha.equals(redisCaptcha);
    }
}